<!doctype html>
<html>

<head>
  <meta charset="utf-8" />
  <title>GIS兵器库</title>
  <link href="lib/markdown/atom-one-dark.css" rel="stylesheet">
  <link href="css/style.css" rel="stylesheet">

  <script src="lib/markdown/marked.min.js"></script>
  <script src="lib/markdown/highlight.min.js"></script>
  <script src="lib/jquery.min.js"></script>
  <script src="lib/jquery.nicescroll.min.js"></script>

  <script src="config_blog.js"></script>
  <script src="config_demo.js"></script>
  <script src="config_source.js"></script>

</head>

<body>
  <nav class="navbar zs_navbar">
    <div class="container">
      <div class="navbar-header">
        <a class="navbar-brand zs_logo" href="index.html">
          <img src="img/logo.png" alt="">
          <span>GIS兵器库</span>
        </a>
      </div>
      <div class="collapse navbar-collapse " id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav">
          <li data-id="wechat">
            <a>公众号</a>
          </li>
          <li data-id="github">
            <a>github</a>
          </li>
        </ul>
      </div>
    </div>
  </nav>
  <div id="content">
    <div id="mainContent"></div>
    <div id="bottomContent">
      <span class="aboutus-span" onclick="contactusClick()">联系我们：CSDN留言或私信</span><br>
      <span class="aboutus-span" onclick="beianClick()">京ICP备20018340号-1</span>
    </div>
  </div>
</body>

<script>
  //配置marked插件
  configMarked();
  init();
  changeSelect()

  //初始化
  function init() {

    $("#content").niceScroll();
    var urlArg = getUrlArgObject();
    if (urlArg && urlArg.blog) {
      loadMarkdown(urlArg.blog)
    } else if (urlArg && urlArg.demo) {
      loadDemo(urlArg.demo)
    } else if (urlArg && urlArg.source) {
      loadSource(urlArg.source)
    } else {
      loadMarkdown("main")
    }
  }

  function contactusClick(){
    window.open('https://blog.csdn.net/gisarmory/')
  }

function beianClick(){
  window.open('https://beian.miit.gov.cn/')
}

  function changeSelect() {
    $(".navbar-nav li").click(function() {
      doChangeSelect(this)
    });
  }

  function doChangeSelect(_this){
    // $(_this).siblings('li').removeClass('active'); // 删除其他兄弟元素的样式
    // $(_this).addClass('active'); // 添加当前元素的样式
    var blogid = $(_this).attr("data-id");
    var blogObj = getBlogConfigById(blogid);
    if(blogObj.href){
      window.open(blogObj.href,"_blank"); 
    }else if(blogObj.url){
      loadMarkdown(blogid);
    }
    

    setTimeout(function(){
      $("#content").niceScroll();
      $("#content").getNiceScroll().resize();
    },200)
  }

  //加载demo示例
  function loadDemo(demoId) {
    var demoObj = getDemoConfigById(demoId);
    if (demoObj) {
      window.location.href = demoObj.url;
    }
  }

  //加载loadSource示例
  function loadSource(sourceId) {
    var sourceObj = getSourceConfigById(sourceId);
    if (sourceObj) {
      window.location.href = sourceObj.url;
    }
  }



  //配置marked插件
  function configMarked() {
    hljs.initHighlightingOnLoad();
    var rendererMD = new marked.Renderer();
    marked.setOptions({
      renderer: rendererMD,
      gfm: true,
      tables: true,
      breaks: false,
      pedantic: false,
      sanitize: false,
      smartLists: true,
      smartypants: false
    });
    marked.setOptions({
      highlight: function(code) {
        return hljs.highlightAuto(code).value;
      }
    });
  }

  //加载markdown文件
  function loadMarkdown(blogid) {
    var blogObj = getBlogConfigById(blogid);
    if (blogObj) {
      xhr = new XMLHttpRequest();
      xhr.open('get', blogObj.url);
      xhr.send('')
      xhr.onload = function() {
        var mdText = xhr.responseText;
        var mdContent = document.getElementById('mainContent');
        //replace是解决代码不显示黑色背景的问题
        mdContent.innerHTML = marked(mdText).replace(/<pre>/g, "<pre class='hljs'>");
      }
    }
  }

  //根据blogid获取blog配置
  function getBlogConfigById(blogId) {
    for (var i = 0; i < blogList.length; i++) {
      var blogObj = blogList[i];
      if (blogObj.id == blogId) {
        return blogObj;
      }
    }
  }

  //根据demoid获取demo配置
  function getDemoConfigById(demoId) {
    for (var i = 0; i < demoList.length; i++) {
      var demoObj = demoList[i];
      if (demoObj.id == demoId) {
        return demoObj;
      }
    }
  }

  //根据sourceid获取source配置
  function getSourceConfigById(sourceId) {
    for (var i = 0; i < sourceList.length; i++) {
      var sourceObj = sourceList[i];
      if (sourceObj.id == sourceId) {
        return sourceObj;
      }
    }
  }

  //返回的是对象形式的参数
  function getUrlArgObject() {
    var args = new Object();
    var query = location.search.substring(1); //获取查询串
    var pairs = query.split(","); //在逗号处断开
    for (var i = 0; i < pairs.length; i++) {
      var pos = pairs[i].indexOf('='); //查找name=value
      if (pos == -1) { //如果没有找到就跳过
        continue;
      }
      var argname = pairs[i].substring(0, pos); //提取name
      var value = pairs[i].substring(pos + 1); //提取value
      args[argname] = unescape(value); //存为属性
    }
    return args; //返回对象
  }
</script>

</html>